<template>
  <view>
    <!-- 搜索框区域 -->
    <view class="search-box">
      <!-- 基本用法 -->
      <uni-search-bar @confirm="search" @input="input" cancelButton='none' bgColor='#fff' radius=100 focus=ture></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResults.length!=0">
      <view class="sugg-item" v-for="(item,i) in searchResults" :key="i"  @click="gotoDetail(item)">
        <view class="goods-name">{{item.goods_name}}</view>
        <!-- uniapp向右图标 -->
        <uni-icons type="forward" size="30"></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text class="titlle-text">搜索历史</text>
        <uni-icons type="trash" size="22" @click="clean"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="hostory-list" >
        <uni-tag :text="item" v-for="(item,i) in histories" :key="i" type="default" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
import { $http } from '@escook/request-miniprogram';
  export default {
    data() {
      return {
        //延时器的延时id
        timer: null,
        //搜索关键词
        kw: '',
        //搜索结果列表
        searchResults: [],
        //存放搜索历史记录
        historyList: ['a',"app","apple"]
      };
    },
    onLoad(){
      //获取到持久化存储的数据
      this.historyList=JSON.parse(uni.getStorageSync('kw')||'[]')
    },
    methods:{
      //input输入事件的函数
      input(e){
        //清除timer对应的延时器
        clearTimeout(this.timer)
        //重新启动一个延时器，并把timerid 赋值给this。timer
       this.timer=setTimeout(()=>{
         //如果 500毫秒内，没有出发新的事件，则为关键词进行赋值
         this.kw=e
         //进行调用搜索
         this.getSearchList()
         this.saveSearchHistory()
       } ,500)
      },
      
      
      // 获取搜索框列表
      async getSearchList(){
        //判断搜索的关键词时候为空,为空则设置结果列表为空
        if(this.kw.length===0){
          this.searchResults=[]
          return
        }
        //发起请求
        const {data: res}= await uni.$http.get('/api/public/v1/goods/qsearch',{query: this.kw})
        console.log(res); 
        //请求失败
        if(res.meta.status!=200){
          return uni.$showMsg();
        }
        this.searchResults=res.message
      },
     
      //点击商品尽心跳转
      gotoDetail(item){
        //进行跳转
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id'+item.goods_id
        })
      },
      //保存搜索历史
      saveSearchHistory(){
        // this.historyList.push(this.kw)
        //创建一个集合对象
        const set=new Set(this.historyList)
        //进行添加元素
        set.add(this.kw)
        //将得到的数组进行转化为arr数组
        this.historyList=Array.from(set)
        
        //对于搜索历史持久化存储
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      
      //点击删除尽心删除所有的历史记录
      clean(){
        this.historyList=[]
        //清楚持久化存储中缓存的数据
        uni.setStorageSync('kw','[]')
      },
      //点击搜索历史跳转到对应的页面
      gotoGoodsList(item){
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?quyery?='+this.kw
        })
      }
    },
  //计算属性
 computed:{
   histories(){
     return [...this.historyList].reverse()
   }
 }
  }
</script>

<style lang="scss">
.search-box{
  background-color: #c00;
  position: sticky;
  top: 0;
  z-index: 1;
}
.sugg-list{
  padding: 0 5px;
  background-color: #fff;
  .sugg-item{
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    .goods-name{
    //文本超出省略号显示
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
.history-box{
  padding: 0 5px;
  .history-title{
    height:80rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .hostory-list{
    display: flex;
    flex-wrap: wrap;
    
    .uni-tag{
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}

</style>
